/*
 * @# HtmlToPdfRender.java 2011-11-25 上午11:25:22
 * 
 * Copyright (C) 2010 - 2011 广州羿安信息科技有限公司
 * Guangzhou ianswer information technology co. ltd.
 * 
 * All rights reserved!
 */
package com.ianswer.experiments.ws.pdf;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.io.IOUtils;
import org.springframework.util.ResourceUtils;
import org.w3c.dom.Document;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import org.xhtmlrenderer.resource.XMLResource;

import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;

/**
 * @author alvin hwang
 */
public class HtmlToPdfRender {

	public static void main(String[] args) throws DocumentException, IOException {
		ITextRenderer renderer = new ITextRenderer();

		String url = "c:/fax.html";
		OutputStream os = new FileOutputStream("c:/fax.pdf");

		Document doc = XMLResource.load(new FileInputStream(new File(url))).getDocument();
		ITextFontResolver fontResolver = renderer.getFontResolver();
		fontResolver.addFont(ResourceUtils.getFile("classpath:simsun.ttc").getAbsolutePath(), BaseFont.IDENTITY_H,
				BaseFont.NOT_EMBEDDED);

		renderer.setDocument(doc, url);
		renderer.layout();

		renderer.createPDF(os);

		IOUtils.closeQuietly(os);
	}
}
